<?php
//No direct acesss
defined('_JEXEC') or die();
jimport('joomla.application.component.model');

class SimplelistingModelListing extends JModel {
	var $_table_prefix = null;

    function __construct()
    {
        parent::__construct();

        global $mainframe, $context;

        $this->_table_prefix = '#__simplelisting';
        $params =&$mainframe->getParams();

    }

    function getSimplelistingLists($id)
    {
        $query = 'SELECT a.*,
        u.name as `username`, u.email as `email`,
        t.name as `type`, ca.name as `category`, l.name as `layout`, fa.name as `facility`,
        cd.telephone as `tel`, cd.mobile `mobile`, cd.fax as `fax`, cd.country as `usercountry`,cd.misc `misc`, ct.name as `country`, et.name as `extra`
        FROM '.$this->_table_prefix.' AS a
        LEFT JOIN '.$this->_table_prefix.'_types AS t ON t.id = a.type_id
        LEFT JOIN '.$this->_table_prefix.'_categories AS ca ON ca.id = a.category_id
        LEFT JOIN '.$this->_table_prefix.'_countries AS ct ON ct.id = a.countries_id
        LEFT JOIN '.$this->_table_prefix.'_extras AS et ON et.id = a.extra_id
        LEFT JOIN '.$this->_table_prefix.'_layouts AS l ON l.id = a.layout_id
        LEFT JOIN '.$this->_table_prefix.'_facilities AS fa ON fa.id = a.facilities
        LEFT JOIN #__users AS u ON u.id = a.create_by
        LEFT JOIN #__contact_details AS cd ON cd.user_id = a.create_by
        WHERE a.published=1 AND a.id = '.$id;
        $db = $this->getDBO();
        $db->setQuery($query);
        $simplelisting = $db->loadObject();

        if ($simplelisting === null)
        JError::raiseError (500, JText::_('LISTING_NOT_FOUND_MSG'));
        else
        return $simplelisting;
    }

    function setFav($id, $user_id)
    {
        $dates = date('Y-m-d');
        $query = "INSERT INTO #__simplelisting_favourite
                (`date_created`,`create_by`,`simplelisting_id`)
                VALUES ( '.$dates.', '.$user_id.', '.$id.' )";
        $db = $this->getDBO();
        $db->setQuery($query);
        if (!$db->query()){
            $errorMessage = $this->getDBO()->getErrorMsg();
            JError::raiseError(500, JText::_('ERROR_ADDING_FAV_MSG').$errorMessage);
        }
    }
}
?>
